import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class demo45 {
    public static void main(String[] args) {
        BlockingQueue<Integer> queue=new ArrayBlockingQueue<>(50);
        Thread producer1=new Thread(()->{
            for(int i=0;i<100;i++){
                try {
                    queue.put(i);
                    //构造供不应求的情况，但二者一开始就会趋于同步
//                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("producer1生产元素"+i);
            }
        },"producer1");
        Thread producer2=new Thread(()->{
            for(int m=100;m<200;m++){
                try {
                    queue.put(m);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("producer2生产元素"+m);
            }
        },"producer2");
        Thread consumer=new Thread(()->{
            int n=0;
//            try {
//                Thread.sleep(200);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
            while(true){
                try {
                    n=queue.take();
                    //构造供大于求的情况
//                    Thread.sleep();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("consumer消费元素"+n);
            }

        },"consumer");
        producer1.start();
//        producer2.start();
        consumer.start();

    }
}
